{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "2de4d35d",
   "metadata": {},
   "source": [
    "# Python 数据类型：整数与浮点数\n",
    "\n",
    "## 1. 整数（int）\n",
    "\n",
    "### 1.1 什么是整数？\n",
    "整数是不带小数部分的数字，可以是正数、负数或零。\n",
    "\n",
    "\n",
    "#### 示例：定义整数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a8ff4c63",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a = 10\n",
      "b = -5\n",
      "c = 0\n"
     ]
    }
   ],
   "source": [
    "a = 10\n",
    "b = -5\n",
    "c = 0\n",
    "\n",
    "print(\"a =\", a)\n",
    "print(\"b =\", b)\n",
    "print(\"c =\", c)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d8db3571",
   "metadata": {},
   "source": [
    "### 1.2 整数运算\n",
    "整数支持基本的数学运算，如加、减、乘、除等。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "c9105642",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x + y = 10\n",
      "x - y = 4\n",
      "x * y = 21\n",
      "x / y = 2.3333333333333335\n",
      "x // y = 2\n",
      "x % y = 1\n",
      "x ** y = 343\n"
     ]
    }
   ],
   "source": [
    "# 示例：整数运算\n",
    "x = 7\n",
    "y = 3\n",
    "\n",
    "# 加法\n",
    "print(\"x + y =\", x + y)\n",
    "\n",
    "# 减法\n",
    "print(\"x - y =\", x - y)\n",
    "\n",
    "# 乘法\n",
    "print(\"x * y =\", x * y)\n",
    "\n",
    "# 除法（结果为浮点数）\n",
    "print(\"x / y =\", x / y)\n",
    "\n",
    "# 整除（结果为整数）\n",
    "print(\"x // y =\", x // y)\n",
    "\n",
    "# 取余\n",
    "print(\"x % y =\", x % y)\n",
    "\n",
    "# 幂运算\n",
    "print(\"x ** y =\", x ** y)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "50e185df",
   "metadata": {},
   "source": [
    "### 1.3 整数类型转换\n",
    "可以使用 int() 函数将其他类型的数据转换为整数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "30c370dc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "int(s) = 123\n",
      "int(f) = 45\n"
     ]
    }
   ],
   "source": [
    "# 示例：整数类型转换\n",
    "s = \"123\"\n",
    "f = 45.67\n",
    "\n",
    "# 字符串转整数\n",
    "print(\"int(s) =\", int(s))\n",
    "\n",
    "# 浮点数转整数（截断小数部分）\n",
    "print(\"int(f) =\", int(f))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cf59c75b",
   "metadata": {},
   "source": [
    "## 2. 浮点数（float）\n",
    "### 2.1 什么是浮点数？\n",
    "浮点数是带小数部分的数字，用于表示实数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "265dcfc6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a = 3.14\n",
      "b = -0.5\n",
      "c = 2.0\n"
     ]
    }
   ],
   "source": [
    "# 示例：定义浮点数\n",
    "a = 3.14\n",
    "b = -0.5\n",
    "c = 2.0\n",
    "\n",
    "print(\"a =\", a)\n",
    "print(\"b =\", b)\n",
    "print(\"c =\", c)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "920254ed",
   "metadata": {},
   "source": [
    "### 2.2 浮点数运算\n",
    "浮点数也支持基本的数学运算。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "02aab807",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x + y = 7.7\n",
      "x - y = 3.3\n",
      "x * y = 12.100000000000001\n",
      "x / y = 2.5\n",
      "x ** y = 42.540042248725975\n"
     ]
    }
   ],
   "source": [
    "# 示例：浮点数运算\n",
    "x = 5.5\n",
    "y = 2.2\n",
    "\n",
    "# 加法\n",
    "print(\"x + y =\", x + y)\n",
    "\n",
    "# 减法\n",
    "print(\"x - y =\", x - y)\n",
    "\n",
    "# 乘法\n",
    "print(\"x * y =\", x * y)\n",
    "\n",
    "# 除法\n",
    "print(\"x / y =\", x / y)\n",
    "\n",
    "# 幂运算\n",
    "print(\"x ** y =\", x ** y)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "05f34d2a",
   "metadata": {},
   "source": [
    "### 2.3 浮点数精度问题\n",
    "由于计算机内部表示浮点数的方式，可能会出现精度问题。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "e17a1a26",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a + b = 0.30000000000000004\n"
     ]
    }
   ],
   "source": [
    "# 示例：浮点数精度问题\n",
    "a = 0.1\n",
    "b = 0.2\n",
    "print(\"a + b =\", a + b)  # 预期输出 0.3，实际输出 0.30000000000000004"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4034822d",
   "metadata": {},
   "source": [
    "### 2.4 浮点数类型转换\n",
    "可以使用 float() 函数将其他类型的数据转换为浮点数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "3cc5c8c9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "float(s) = 123.45\n",
      "float(i) = 100.0\n"
     ]
    }
   ],
   "source": [
    "# 示例：浮点数类型转换\n",
    "s = \"123.45\"\n",
    "i = 100\n",
    "\n",
    "# 字符串转浮点数\n",
    "print(\"float(s) =\", float(s))\n",
    "\n",
    "# 整数转浮点数\n",
    "print(\"float(i) =\", float(i))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
